package org.sudoku.solver;

import static org.sudoku.solver.Sudoku.COL;
import static org.sudoku.solver.Sudoku.ROW;

/**
 * This is an extension of the
 * 
 * @see org.sudoku.solver.ProbabilisticSolver in the sense that the defintion of
 *      inference is modified to a 2-level inference. Meaning, 2-level
 *      probability technique is applied if there are no 1-level inference is
 *      found. So
 * @see ProbabilisticInferenceSolver#infer() method will always return 1 (i.e.
 *      whenever 2-level inference technique does find at least one cell) until
 *      the problem is solved.
 * @author shivkumar cm
 * 
 */
public class ProbabilisticInferenceSolver extends ProbabilisticSolver implements
		I9X9Solver {

	@Override
	public boolean infer() {

		boolean oneLevelInference = super.infer();

		if (oneLevelInference) {
			return true;
		}

		// A 2-level probabilistic solver - assumes that there will be atleast
		// one cell for which we can say that it has one of two given digits
		int arr[] = getCellWithOptions(2);

		// Unable to solve
		if (arr == null) {
			System.out.println("Probabilistic solver failed...");
			return false;
		}
		int row = arr[ROW], col = arr[COL], digit1 = arr[2], digit2 = arr[3];
		if (assume(row, col, digit1)) {
			System.out
					.println("Assumption correct!!! The dry run has been copied over.");
			return true;
		} else {
			System.out.printf(
					"Assumption wrong. Position [%d, %d] must be %d.\n", row,
					col, digit2);
			m_inferred.add(new int[] { row, col, digit2 });
			return true;
		}
	}
}
